package com.icloud.framework.query;

import java.util.*;

public abstract class TreeNode<T extends TreeNode> extends AbstractDTO {

    public abstract Integer findSortValue();

    public abstract List<T> findChildren();

    public abstract String findKey();

    public abstract String findParentId();



    public void sortChildren() {
        Collections.sort(findChildren(), new Comparator<T>() {
            @Override
            public int compare(T node1, T node2) {
                int result = 0;

                Integer ordby1 = node1.findSortValue();
                Integer ordby2 = node2.findSortValue();

                if (null != ordby1 && null != ordby2) {
                    result = (ordby1 < ordby2 ? -1 : (ordby1 == ordby2 ? 0 : 1));
                } else {
                    return 0;
                }
                return result;
            }

        });
        // 对每个节点的下一层节点进行排序
        for (Iterator<T> it = findChildren().iterator(); it.hasNext(); ) {
            it.next().sortChildren();
        }
    }

}
